//
//  JSLLoanController.m
//  JingleSpeedLoan
//
//  Created by 宋质彬 on 2018/6/19.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLLoanController.h"
#import "JSLProductCell.h"
#import "JSLLoanDetialController.h"
@interface JSLLoanController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalPage;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@end

@implementation JSLLoanController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"产品大全";
    [self tableView];
    
    [self refreshData];
}


- (void)getDataSourceWithFlag:(NSInteger)flag {
    
    if (flag == 0) {
        _pageNum = 1;
        
    }else if (flag == 1){
        
        if (_pageNum >= 1) {
            
            _pageNum = _pageNum +1;
        }
    }
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    parameters[@"publishApp"] = @"2";
//    parameters[@"productType"] = @"4";
////    parameters[@"productId"] = @"";
    parameters[@"pageNum"] = [NSString stringWithFormat:@"%ld",(long)_pageNum];
    parameters[@"pageSize"] = @"15";
    
    [HttpRequest POST:HOME_ALL_PRODUCT_URL parameters:parameters success:^(id  _Nullable result, NSString * _Nullable successMessage) {
        NSLog(@"%@",result);

        NSArray *listArray = result[@"data"][@"productList"];
        
        self.arrDataSource = [NSMutableArray arrayWithArray:listArray];

//        if (flag == 0) {
//            
//            if (listArray.count == 0) {
//                [self.arrDataSource removeAllObjects];
//            }else{
//                self.arrDataSource = [NSMutableArray arrayWithArray:listArray];
//            }
//            // 缓存前15条数据
//            [CacheManager setObject:self.arrDataSource forKey:@"SYJ_TRADE_LIST"];
//           
//        }else if (flag == 1){
//            
//            if (listArray.count == 0) {
//                [self showMessage:@"没有更多数据"];
//            }else{
//                // 下拉刷新，flag = 1;加载更老的数据。往数组里面添加新数据
//                NSMutableArray *arrr= [NSMutableArray arrayWithArray:listArray];
//                
//                [self.arrDataSource addObjectsFromArray:arrr];
//            }
//        }
//        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSInteger code, NSString * _Nullable errorMessage) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showMessage:errorMessage];
    }];
    
}

#pragma mark - 请求数据
- (void)refreshData {
    
    NSArray * arr = [CacheManager getObject:@"SYJ_TRADE_LIST"];
    self.arrDataSource = [NSMutableArray arrayWithArray:arr];
    [self.tableView reloadData];
    
    [self getDataSourceWithFlag:0];

}

- (void)getMoreData {
    
    [self getDataSourceWithFlag:1];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrDataSource.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JSLProductCell *cell = [JSLProductCell cellWithTableView:tableView];
    [cell configProductCellContent:self.arrDataSource[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JSLLoanDetialController *loanDetial = [[JSLLoanDetialController alloc] init];
    [loanDetial configLoanDetial:self.arrDataSource[indexPath.row] fromFlag:LoanFromNormal];
    [self pushViewController:loanDetial animated:YES];
}

#pragma mark - get
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-64 -49 ) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.scrollEnabled = YES;
        _tableView.rowHeight = 100 *AutoSizeScaleYY +10*AutoSizeScaleYY;
        _tableView.backgroundColor = BACKGROUND_COLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];

        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
        
        _tableView.tableHeaderView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
        
    }
    
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
